package freemind.controller;

import javax.swing.event.TreeModelEvent;
import javax.swing.event.TreeModelListener;

import freemind.modes.browsemode.BrowseNodeModel;
import freemind.view.mindmapview.NodeView;

public class CustomListener implements TreeModelListener {

	private CustomControllerBase customController;
	
	public CustomListener(CustomControllerBase custom){
		this.customController = custom;
	}
	
	public void treeNodesChanged(TreeModelEvent e) {
		// TODO Auto-generated method stub
		
	}

	public void treeNodesInserted(TreeModelEvent e) {
		// TODO Auto-generated method stub
		
	}

	public void treeNodesRemoved(TreeModelEvent e) {

		for( Object o:e.getChildren() ){
			BrowseNodeModel node = (BrowseNodeModel) o;
			
			
			if( node.getAdapter().isACopy() ){
				
				BrowseNodeModel copiedNode = customController.searchNodeById( customController.model.getRoot(), node.getAdapter().getCopiaId());
				copiedNode.getAdapter().setAsNotCopied();
				customController.model.changeNode(copiedNode, copiedNode.getAdapter().getTexto());
			}
			else if( node.getAdapter().isCopied()){
				BrowseNodeModel nodeClone = customController.searchNodeById( customController.model.getRoot(), node.getAdapter().getCloneId() );
				
				nodeClone.getAdapter().setAsNotCopy();
				customController.model.removeNodeFromParent(nodeClone);					
			}
			
		}
		
	}

	public void treeStructureChanged(TreeModelEvent e) {
		// TODO Auto-generated method stub
		
	}

}
